package com.keyinong.jiashiyuan.view;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Address;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.keyinong.jiashiyuan.R;
import com.keyinong.jiashiyuan.adapter.DingdanAdapter;
import com.keyinong.jiashiyuan.entity.DingdanEntity;
import com.keyinong.jiashiyuan.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

public class DingdanFramgment extends Fragment{
	private DingdanAdapter dingdanadapter;
	/**订单列表*/
	private ListView lv;
	/**获得数据*/
	ArrayList<DingdanEntity> infolist;
	HttpTools httptools=null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_dingdan, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		lv=(ListView)getView().findViewById(R.id.lv_dingdan);
		Dingdan();
	}
	private void Dingdan(){
		infolist=new ArrayList<DingdanEntity>();
		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("token", "");
//		Log.e("shi", "token---" + token);
		if (httptools == null) {
			httptools = new HttpTools(getActivity());
		}
		httptools.Dingdan(token, new JsonDingdan());
	}
	public class JsonDingdan extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
// 			 Log.i("info",response.toString());
				boolean code = response.getBoolean("code");
				String msg = response.getString("msg");
				if(code==true){
					JSONArray array = response.getJSONArray("data");
				for (int i = 0; i < array.length(); i++) {
					JSONObject data = array.getJSONObject(i);
					String shopname = data.getString("shopname");
					String cellphone = data.getString("cellphone");
					String address = data.getString("address");
					String id = data.getString("id");
					String state = data.getString("state");
					String late_time = data.getString("late_time");
					String early_time = data.getString("early_time");
					String lng =  data.getString("lng");
					String lat =  data.getString("lat");
					infolist.add(new DingdanEntity(id, state, shopname, cellphone, address, early_time,late_time, lng, lat));
				}
				dingdanadapter=new DingdanAdapter(getActivity(), infolist);
				lv.setAdapter(dingdanadapter);
//				Log.i("zq", "dingdan----" + response.toString());
				} else {
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
				}
				
			}catch(JSONException e){
				e.printStackTrace();
			}
		}
	}
}
